#include "UnfoldArrayButton.h"
#include "EntryFrameItem.h"
#include <QPainter>
#include <QCursor>

UnfoldArrayButton::UnfoldArrayButton(EntryFrameItem* parent)
: QGraphicsItem(parent)
{
	this->_frame = parent;
	this->_isHover = false;
	this->setAcceptHoverEvents(true);
	this->setFlag(QGraphicsItem::ItemIsFocusable, true);	
}

UnfoldArrayButton::~UnfoldArrayButton(void)
{
}

void UnfoldArrayButton::paint( QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget )
{

	QImage icon;
	int x = 5;
	// 假如_frame裡面裝的是ref，表示_frame有unfold icon，則unfoldArrayButton就要再往右邊
	if (_frame->getIcon()!=NULL){
		x = x + 11;
	}
	if ( _frame->getExpanded() ) {
		icon = QImage(":/resource/expand.png");
		painter->drawImage(x,10,icon);
	} else {
		icon = QImage(":/resource/unfold.png");
		painter->drawImage(x,8,icon);
	}
}

QRectF UnfoldArrayButton::boundingRect() const
{
	return QRectF( 5,8,16,16 );
}

void UnfoldArrayButton::hoverEnterEvent( QGraphicsSceneHoverEvent* event)
{
	this->_isHover = true;
	this->setCursor(Qt::PointingHandCursor);
	this->update();
}

void UnfoldArrayButton::hoverLeaveEvent( QGraphicsSceneHoverEvent* event )
{
	this->_isHover = false;
	this->setCursor(Qt::ArrowCursor);
	this->update();
}

void UnfoldArrayButton::mousePressEvent( QGraphicsSceneMouseEvent* event)
{
	_frame->setExpanded( !(_frame->getExpanded()) );

	if ( _frame->getExpanded()){
		this->setToolTip("Fold Array");
	} else {
		this->setToolTip("Unfold Array");
	}
}
